using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using 道路运输从业人员无纸化考试系统.Properties;

namespace 道路运输从业人员无纸化考试系统
{
    public static class GlobamParms
    {
        public static bool isuse = true;

        public static string name = "";

        public static string code = "";

        public static string type = "";

        public static Form firstform = null;

        public static FormWindowState firstformstate = FormWindowState.Normal;

        public static Form mainform = null;

        public static FormWindowState mainformstate = FormWindowState.Normal;

        public static Form sxlxform = null;

        public static FormWindowState sxlxformstate = FormWindowState.Normal;

        public static Form sjlxform = null;

        public static FormWindowState sjlxformstate = FormWindowState.Normal;

        public static string isWhere = "";

        public static string Pmode = "";

        public static string ZJ = "";

        public static string QJ = "";

        public static Form xzzjform = null;

        public static Form loginform = null;

        public static DataSet ds;

        private static byte[] Keys = new byte[8]
        {
            18,
            52,
            86,
            120,
            144,
            171,
            205,
            239
        };

        public static Stream GetAssemblyStream()
        {
            return GetAssemblyStream("data.DataXMLFile.xml");
        }
        public static Stream GetAssemblyStream(string fileName)
        {
            Assembly asm = Assembly.GetExecutingAssembly();
            return asm.GetManifestResourceStream(fileName);
        }

        public static Image GetAssmblyImg(string fileName)
        {
            Stream sm = GetAssemblyStream(fileName);
            if (sm == null)
                return null;

            return Image.FromStream(sm);
        }

        public static DataTable ReadXmlToDataTable(Stream sm, string TagName, string[] NodeAttributs)
        {
            DataTable dt = new DataTable();
            for (int j = 0; j < NodeAttributs.Length; j++)
            {
                dt.Columns.Add(NodeAttributs[j], typeof(string));
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(sm);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName(TagName);
            foreach (XmlNode node in nodeList)
            {
                DataRow dr = dt.NewRow();
                for (int j = 0; j < NodeAttributs.Length; j++)
                {
                    dr[NodeAttributs[j]] = node.ChildNodes[j].InnerText.ToString();
                }

                dt.Rows.Add(dr);


            }
            return dt;
        }

        public static DataTable ReadXmlToDataTable(string filePath, string TagName, string[] NodeAttributs)
        {
            DataTable dt = new DataTable();
            for (int j = 0; j < NodeAttributs.Length; j++)
            {
                dt.Columns.Add(NodeAttributs[j], typeof(string));
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filePath);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName(TagName);
            foreach (XmlNode node in nodeList)
            {
                DataRow dr = dt.NewRow();
                for (int j = 0; j < NodeAttributs.Length; j++)
                {
                    dr[NodeAttributs[j]] = node.ChildNodes[j].InnerText.ToString();
                }

                dt.Rows.Add(dr);


            }
            return dt;
        }

        public static DataTable GetZhangjie()
        {
            string[] attributs = new string[1]
            {
                "zhangjie"
            };
            DataTable dt2 = null;
            //dt2 = ReadXmlToDataTable(Application.StartupPath + "\\data\\DataXMLFile.xml", "zhangjie", attributs);
            dt2 = GlobamParms.ReadXmlToDataTable(GlobamParms.GetAssemblyStream(), "zhangjie", attributs);
            return SelectDistinct("tb", dt2, "zhangjie");
        }

        public static bool ColumnEqual(object A, object B)
        {
            if (A == DBNull.Value && B == DBNull.Value)
            {
                return true;
            }
            if (A == DBNull.Value || B == DBNull.Value)
            {
                return false;
            }
            return A.Equals(B);
        }

        public static DataTable SelectDistinct(string TableName, DataTable SourceTable, string FieldName)
        {
            DataTable dt = new DataTable(TableName);
            dt.Columns.Add(FieldName, SourceTable.Columns[FieldName].DataType);
            object LastValue = null;
            DataRow[] array = SourceTable.Select("", FieldName);
            foreach (DataRow dr in array)
            {
                if (LastValue == null || !ColumnEqual(LastValue, dr[FieldName]))
                {
                    LastValue = dr[FieldName];
                    dt.Rows.Add(LastValue);
                }
            }
            if (ds != null)
            {
                ds.Tables.Add(dt);
            }
            return dt;
        }

        public static int GetCount()
        {
            string[] attributs = new string[1]
            {
                "no"
            };
            DataTable dt2 = null;
            //dt2 = ReadXmlToDataTable(Application.StartupPath + "\\data\\DataXMLFile.xml", "no", attributs);
            dt2 = GlobamParms.ReadXmlToDataTable(GlobamParms.GetAssemblyStream(), "no", attributs);
            return dt2.Rows.Count;
        }

        public static void label_MouseEnter(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            lb.Image = Resources.quan;
        }

        public static void label_MouseLeave(object sender, EventArgs e)
        {
            Label lb = sender as Label;
            lb.Image = null;
        }

        public static void labelClick(Panel p, Label l)
        {
            for (int i = 0; i < p.Controls.Count; i++)
            {
                Label lb = p.Controls[i] as Label;
                lb.MouseEnter -= label_MouseEnter;
                lb.MouseLeave -= label_MouseLeave;
                if (l != lb)
                {
                    lb.Image = null;
                    lb.MouseEnter += label_MouseEnter;
                    lb.MouseLeave += label_MouseLeave;
                }
            }
        }

        public static DataTable GetSystems()
        {
            string[] attributs = new string[2]
            {
                "sysname",
                "sysvalue"
            };
            DataTable dt = null;
            return ReadXmlToDataTable(Application.StartupPath + "\\data\\SystemXMLFile.xml", "system", attributs);
            //return ReadXmlToDataTable(GetAssemblyStream("data.SystemXMLFile.xml"), "system", attributs);
        }

        public static string GetStr(string str)
        {
            string newStr = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (!newStr.Contains(str[i].ToString()))
                {
                    newStr += str[i].ToString();
                }
            }
            return newStr;
        }

        public static DataTable StorData(DataTable dt)
        {
            DataTable dtAll = new DataTable();
            dt.DefaultView.RowFilter = " type='判断题' ";
            DataTable dtp = randomData(CreateTable(dt.DefaultView));
            dt.DefaultView.RowFilter = " type='单选题' ";
            DataTable dtx = randomData(CreateTable(dt.DefaultView));
            dt.DefaultView.RowFilter = " type='多选题' ";
            DataTable dtd = randomData(CreateTable(dt.DefaultView));
            dtAll.Merge(dtp);
            dtAll.Merge(dtx);
            dtAll.Merge(dtd);
            return dtAll;
        }

        private static DataTable randomData(DataTable ldt)
        {
            Random r = new Random();
            for (int MAX = ldt.Rows.Count - 1; MAX > 0; MAX--)
            {
                int newPos = r.Next(0, MAX);
                DataRow dr = ldt.NewRow();
                dr.ItemArray = ldt.Rows[newPos].ItemArray;
                ldt.Rows[newPos].ItemArray = ldt.Rows[MAX].ItemArray;
                ldt.Rows[MAX].ItemArray = dr.ItemArray;
            }
            return ldt;
        }

        public static DataTable CreateTable(DataView obDataView)
        {
            if (null == obDataView)
            {
                throw new ArgumentNullException("DataView", "Invalid DataView object specified");
            }
            DataTable obNewDt = obDataView.Table.Clone();
            int idx = 0;
            string[] strColNames = new string[obNewDt.Columns.Count];
            foreach (DataColumn col in obNewDt.Columns)
            {
                strColNames[idx++] = col.ColumnName;
            }
            IEnumerator viewEnumerator = obDataView.GetEnumerator();
            while (viewEnumerator.MoveNext())
            {
                DataRowView drv = (DataRowView)viewEnumerator.Current;
                DataRow dr = obNewDt.NewRow();
                try
                {
                    string[] array = strColNames;
                    foreach (string strName in array)
                    {
                        dr[strName] = drv[strName];
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                obNewDt.Rows.Add(dr);
            }
            return obNewDt;
        }

        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
    }
}
